﻿#include "UiSystem.h"

//----------------------------------------------------------------------------------
UiSystem::UiSystem()
{
	m_pUiRender = nullptr;
	m_pActiveSheet = nullptr;
}
//----------------------------------------------------------------------------------
UiSystem::~UiSystem()
{

}
//----------------------------------------------------------------------------------
bool UiSystem::Initialization(SceneManager *pSceneMgr)
{
	m_pSceneMgr = pSceneMgr;

	m_pUiRender = &CEGUI::Direct3D10Renderer::bootstrapSystem(m_pSceneMgr->m_pD3dDevice);

	CEGUI::DefaultResourceProvider* pResourceProvider = static_cast<CEGUI::DefaultResourceProvider*>
		(CEGUI::System::getSingleton().getResourceProvider());

	//设置资源目录
	pResourceProvider->setResourceGroupDirectory("schemes", "Media/ceguidatafiles/schemes/");
	pResourceProvider->setResourceGroupDirectory("imagesets","Media/ceguidatafiles/imagesets/");
	pResourceProvider->setResourceGroupDirectory("fonts", "Media/ceguidatafiles/fonts/");
	pResourceProvider->setResourceGroupDirectory("layouts", "Media/ceguidatafiles/layouts/");
	pResourceProvider->setResourceGroupDirectory("looknfeels","Media/ceguidatafiles/looknfeel/");
	pResourceProvider->setResourceGroupDirectory("lua_scripts","Media/ceguidatafiles/lua_scripts/");

	CEGUI::Imageset::setDefaultResourceGroup("imagesets");
	CEGUI::Font::setDefaultResourceGroup("fonts");
	CEGUI::Scheme::setDefaultResourceGroup("schemes");
	CEGUI::WidgetLookManager::setDefaultResourceGroup("looknfeels");
	CEGUI::WindowManager::setDefaultResourceGroup("layouts");
	CEGUI::ScriptModule::setDefaultResourceGroup("lua_scripts");

	//设置主题，字体
	CEGUI::Logger::getSingleton().setLoggingLevel(CEGUI::LoggingLevel::Informative);
	CEGUI::SchemeManager::getSingleton().create("TaharezLook.scheme");
	CEGUI::System::getSingleton().setDefaultMouseCursor("TaharezLook", "MouseArrow");
	CEGUI::MouseCursor::getSingleton().setImage("TaharezLook", "MouseArrow");
	CEGUI::System::getSingleton().setDefaultFont("DejaVuSans-10");

	if (!LoadUiLayout())
	{
		return false;
	}

	return true;
}
//----------------------------------------------------------------------------------
void UiSystem::Render(float fElapsedTime)
{
	UpdateFPS();
	CEGUI::System& ceguiSystem = CEGUI::System::getSingleton();
	ceguiSystem.injectTimePulse(fElapsedTime / 1000.0f);

	ceguiSystem.renderGUI();
}
//----------------------------------------------------------------------------------
void UiSystem::Release()
{
	if (m_pUiRender != nullptr)
	{
		m_pUiRender->destroySystem();
		m_pUiRender =nullptr;
	}
}
//----------------------------------------------------------------------------------
bool UiSystem::LoadUiLayout()
{
	CEGUI::WindowManager &winMgr = CEGUI::WindowManager::getSingleton();
	winMgr.destroyWindow(m_pActiveSheet);
	m_pActiveSheet = winMgr.loadWindowLayout("main.layout");
	CEGUI::System::getSingleton().setGUISheet(m_pActiveSheet);

	//设置UI的版本信息：
	std::string strVersion;
	strVersion = "R02";
	winMgr.getWindow("staticTextVersion")->setText("Version:" + strVersion);

	//绑定buttonExit按钮事件
	winMgr.getWindow("buttonExit")->subscribeEvent(
		CEGUI::Window::EventMouseButtonUp, 
		CEGUI::Event::Subscriber(&UiSystem::EventMainExit, this));

	return true;
}
//----------------------------------------------------------------------------------
bool UiSystem::EventMainExit(const CEGUI::EventArgs& args)
{
	PostQuitMessage(0);
	return true;
}
//----------------------------------------------------------------------------------
void UiSystem::UpdateFPS()
{
	CEGUI::WindowManager &winMgr = CEGUI::WindowManager::getSingleton();

	std::stringstream strmFps;
	strmFps<<m_pSceneMgr->m_fFps;
	std::string strFps = strmFps.str();
	winMgr.getWindow("staticTextFps")->setText("FPS:" + strFps);
}